const jwt = require('koa-jwt');
const Router = require('koa-router');
const router = new Router({prefix:'/users'});
const {find,findById,create,update,delete:del,login,
      checkOwner, checkUserExist,listFollowing,listFollowers,follow,unfollow,
      listFollowingTopic,followTopic,unfollowTopic,listQuestions,
      listLikingAnswers,likeAnswer,unlikeAnswer,
      listDisLikingAnswers,dislikeAnswer,undislikeAnswer,
      listCollectingAnswers,CollectingAnswers,unCollectingAnswer
      } = require('../controllers/users')

const { checkTopicExist } = require('../controllers/topics')
const { checkAnswerExist } = require('../controllers/answers')
const {secret} = require('../config')
//认证
const auth = jwt({secret});

router.get('/',find);
router.post('/',create);
router.get('/:id',findById);
router.patch('/:id',auth,checkOwner,update);
router.delete('/:id',auth,checkOwner,del);
router.post('/login',login);

router.get('/:id/following',listFollowing);  //get 获取我关注的
router.get('/:id/following',listFollowers);  //get 获取粉丝
router.put('/following/:id',auth,checkUserExist,follow)    //put 关注
router.delete('/following/:id',auth,checkUserExist,unfollow)    //delete 取消关注

router.get('/:id/followingTopics',listFollowingTopic);  //get 获取我关注的话题
router.put('/followingTopics/:id',auth,checkTopicExist,followTopic)    
router.delete('/followingTopics/:id',auth,checkTopicExist,unfollowTopic)

router.get('/:id/questions',listQuestions);  //get 获取粉丝

router.get('/:id/likingAnswers',listLikingAnswers);  //点赞
router.put('/likingAnswers/:id',auth,checkAnswerExist,likeAnswer,undislikeAnswer)  //互斥
router.delete('/likingAnswers/:id',auth,checkAnswerExist,unlikeAnswer)

router.get('/:id/dislikingAnswers',listDisLikingAnswers);  //踩
router.put('/dislikingAnswers/:id',auth,checkAnswerExist,dislikeAnswer,unlikeAnswer)  //互斥
router.delete('/dislikingAnswers/:id',auth,checkAnswerExist,undislikeAnswer)

router.get('/:id/collectingAnswers',listCollectingAnswers);  //收藏
router.put('/collectingAnswers/:id',auth,checkAnswerExist,CollectingAnswers)
router.delete('/collectingAnswers/:id',auth,checkAnswerExist,unCollectingAnswer)

module.exports = router;   